package org.congchinh.example;
import javax.jcr.Credentials;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;

import org.exoplatform.services.jcr.RepositoryService;
import org.exoplatform.services.rest.HTTPMethod;
import org.exoplatform.services.rest.OutputTransformer;
import org.exoplatform.services.rest.Response;
import org.exoplatform.services.rest.URIParam;
import org.exoplatform.services.rest.URITemplate;
import org.exoplatform.services.rest.container.ResourceContainer;
import org.exoplatform.services.rest.transformer.StringOutputTransformer;
@URITemplate("/nodee/")
@OutputTransformer(StringOutputTransformer.class)
public class HelloExo implements ResourceContainer {
	private final RepositoryService repositoryService;	
	private boolean checked;
	public HelloExo(RepositoryService res) {
		repositoryService = res;		
		checked = false;
	}
	
	public void init() {
		Session session = getSession();		
		try {
			if (!session.getRootNode().hasNode("a")) {
				session.getRootNode().addNode("a");
				session.save();
				System.out.println("add ok");
			}			
		}
		catch (Exception e) {
			e.printStackTrace();
			System.out.println("add fail");
		}
		finally {
			session.logout();
		}
		checked = true;
	}
	
	
	@URITemplate("/get/")
	@HTTPMethod("GET")
	public Response getNode() {
		if (!checked) {
			System.out.println("ex, init cai' de^^^");
			init();
		}
		StringBuffer sb = new StringBuffer("<nodes>\n");
		Session session = getSession();
		try {
			Node myNode = session.getRootNode().getNode("a");
			NodeIterator nodes = myNode.getNodes();
			while (nodes.hasNext()) {
				Node node = nodes.nextNode();
				sb.append("<node id ='" + node.getUUID() + "' ");
				sb.append("name = '" + node.getName() + "' />\n");				
			}
		}
		catch(Exception e) {
			System.out.println("ex, error..");
		}
		sb.append("</nodes>");
		return Response.Builder.ok(sb.toString(), "text/plain").build();
	}
	
	@URITemplate("/remove/{nodeName}/")
	@HTTPMethod("GET")
	public Response removeNode(@URIParam("nodeName") String nodeName) {
		if (!checked) {
			System.out.println("ex, init cai' nao` :)");
		}
		Boolean ok = false;
		StringBuffer sb = new StringBuffer("<status>");
		Session session = getSession();
		try {
			Node myNode = session.getRootNode().getNode("a");
			NodeIterator nodes = myNode.getNodes();
			while(nodes.hasNext() && !ok) {
				Node node = nodes.nextNode();
				if (nodeName.equals(node.getName())) {
					node.remove();
					sb.append("Xoa thanh cong node: " + nodeName);
					System.out.println("ex, xoa oki node: " + nodeName);
					ok = true;
				}
			}
		}
		catch(Exception e) {
			System.out.println("ex, loi~ j nua day");
		}
		if (!ok) {
			sb.append("Ex, ko tim thay node, sao xoa dc, exx");
			System.out.println("ex, ko tim thay node de xoa");
		}
		sb.append("</status>");
		return Response.Builder.ok(sb.toString(), "text/plain").build();
	}
	
	@URITemplate("/add/{parentNode}/{nodeName}/")
	@HTTPMethod("GET")	
	public Response addNode(
			@URIParam("parentNode") String parentNode,
			@URIParam("nodeName") String nodeName) 
	{
		if (!checked) {
			System.out.println("ex, init cai' de^^^");
			init();
		}
		StringBuffer sb = new StringBuffer("<status>");
		Session session = getSession();
		Boolean added = false;
		try {
			Node myNode = session.getRootNode().getNode("a");
			NodeIterator nodes = myNode.getNodes();
			if (!nodes.hasNext()) {
				myNode.addNode(nodeName);				
			}
			while (nodes.hasNext() && !added) {
				Node node = nodes.nextNode();
				if (parentNode.equals(node.getName())) {
					node.addNode(nodeName);					
				}
			}
			session.save();
			added = true;
			sb.append("ex, add oki");
			System.out.println("ex, add oki");		
		}
		catch(Exception e) {
			e.printStackTrace();
			System.out.println("ex, add fail");
		}
		if (!added) {
			sb.append("ex, ko tim thay node: " + parentNode);
			System.out.println("ex, ko tim thay node: " + parentNode);
		}
		sb.append("</status>");
		session.logout();
		return Response.Builder.ok(sb.toString(), "text/plain").build();
	}
	
	public Session getSession() {
		Credentials credentials = new SimpleCredentials("root", "exo".toCharArray());
		try {
			Repository repository = repositoryService.getRepository("repository");
			Session session = repository.login(credentials, "system");
			System.out.println("A session has just been created!");
			return session;
		} catch(Exception e) {
			e.printStackTrace();			
			return null;
		}
	}
}
